/**
 * 
 */
package com.module.bp.condition;

import com.dalonedrau.d20.Diceroller;
import com.module.bp.player.BPConsts;

/**
 * A {@link Condition} that passes only with a sucessful die roll.  Modifiers
 * can also be applied to the roll.
 * @author Donald
 *
 */
public class RollCondition implements Condition {
	/** 
	 * condition modifier to the roll - only applied to the 
	 * roll if the associated condition passes. 
	 */
	private int conditionModifier;
	/** the {@link Condition} associated with the condition modifier. */
	private Condition condition;
	/** the modifier to the die roll. */
	private int modifier;
	/** 
	 * the threshold for the die roll; if the roll equals 
	 * or exceeds the threshold, the condition is passed. 
	 */
	private int threshold;
	/** 
	 * Creates a new instance of {@link RollCondition} 
	 * that passes automatically. 
	 */
	public RollCondition() { this(0, 0); }
	/**
	 * Creates a new instance of {@link RollCondition}.
	 * @param condMod condition modifier to the roll
	 * @param cond the {@link Condition} that must pass in 
	 * order to apply the condition modifier to the roll. 
	 * if this is null, then the condition modifier is 
	 * never applied
	 * @param mod any modifiers applied to the die roll
	 * @param thresh the threshold for the die roll; if 
	 * the roll equals or exceeds the threshold, the 
	 * condition is passed
	 */
	public RollCondition(final int condMod, final Condition cond, 
			final int mod, final int thresh) {
		conditionModifier = condMod;
		condition = cond;
		modifier  = mod;
		threshold = thresh;
	}
	/**
	 * Creates a new instance of {@link RollCondition} with 
	 * no associated condition modifiers.
	 * @param mod any modifiers applied to the die roll
	 * @param thresh the threshold for the die roll; if 
	 * the roll equals or exceeds the threshold, the 
	 * condition is passed
	 */
	public RollCondition(final int mod, final int thresh) { 
		this(0, null, mod, thresh); 
	}
	/**
	 * {@inheritDoc}
	 */
	public final boolean passes() {
		boolean rollPasses = false;
		// add condition modifier to the roll if and only if a 
		// condition has been defined and is passed
		if (condition != null 
				&& condition.passes()) { 
			modifier += conditionModifier;
		}
		int roll = Diceroller.getInstance().rolldX(BPConsts.DIE_ROLL);
		roll += modifier;
		// if the roll equals or exceeds the threshold, 
		// then the condition passes
		if (roll >= threshold) {
			System.out.println("passes with roll"+roll);
			rollPasses = true;
		}
		return rollPasses;
	}
	/**
	 * Sets the condition modifier to the roll.
	 * @param val the condition modifier to the roll - only applied if there 
	 * is an associated condition and that condition passes
	 */
	public final void setCdtnModifier(final int val) { 
		conditionModifier = val; 
	}
	/**
	 * Sets the {@link Condition} for the condition modifier to the roll.
	 * @param val the associated {@link Condition} for the condition modifier
	 */
	public final void setCondition(final Condition val) { condition = val; }
	/**
	 * Sets the modifier to the die roll.
	 * @param val any modifiers applied to the die roll
	 */
	public final void setModifier(final int val) { modifier = val; }
	/**
	 * Sets the threshold needed to pass the die roll.
	 * @param val threshold for the roll
	 */
	public final void setThreshold(final int val) { threshold = val; }
}
